package com.mxnet.chongjin.supoman.style;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.mxnet.chongjin.supoman.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chongjin on 2017/10/24.
 */

public class XDialog extends Dialog implements View.OnClickListener, View.OnTouchListener, PickerView.onSelectListener {
    private Context context;
    // 布局文件id
    private int layoutResID;
    // 要监听OnClickListener的控件id
    private int[] listenedItems;
    //要建厅OnTouchEvent的空间id
    private int[] touchedItems;
    private int[] selectItems;

    public XDialog(Context context, int layoutResID, int[] listenedItems) {
        //dialog的样式
        super(context, R.style.dialog_custom);
        this.context = context;
        this.layoutResID = layoutResID;
        this.listenedItems = listenedItems;
    }

    public XDialog(Context context, int layoutResID, int[] listenedItems, int[] touchedItems) {
        //dialog的样式
        super(context, R.style.dialog_custom);
        this.context = context;
        this.layoutResID = layoutResID;
        this.listenedItems = listenedItems;
        this.touchedItems = touchedItems;
    }

    public XDialog(Context context, int layoutResID, int[] listenedItems, int[] touchedItems, int[] selectItems) {
        //dialog的样式
        super(context, R.style.dialog_custom);
        this.context = context;
        this.layoutResID = layoutResID;
        this.listenedItems = listenedItems;
        this.touchedItems = touchedItems;
        this.selectItems = selectItems;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        //设置dialog显示位置居中
        window.setGravity(Gravity.BOTTOM);
        // 添加动画效果
        window.setWindowAnimations(R.style.bottom_menu_animation);
        setContentView(layoutResID);

        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        // 设置dialog宽度为屏幕的
        lp.width = display.getWidth();
        getWindow().setAttributes(lp);
        // 点击Dialog外部消失
        setCanceledOnTouchOutside(true);
        //遍历控件id,添加点击事件
        for (int id : listenedItems) {
            findViewById(id).setOnClickListener(this);
        }

        if (touchedItems != null) {
            for (int id : touchedItems) {
                findViewById(id).setOnTouchListener(this);
            }
        }


        List<String> data = new ArrayList<String>();
        List<String> seconds = new ArrayList<String>();
        List<String> workTime = new ArrayList<String>();
        for (int i = 0; i < 24; i++) {
            data.add(i < 10 ? "0" + i : "" + i);
        }
        for (int i = 0; i < 60; i++) {
            seconds.add(i < 10 ? "0" + i : "" + i);
        }
        for (int i = 0; i <= 120; i += 5) {
            workTime.add(i < 10 ? "0" + i : "" + i);
        }
        if (selectItems != null) {
            for (int id : selectItems) {
                ((PickerView) findViewById(id)).setOnSelectListener(this);
                if (id == R.id.pv_left) {
                    ((PickerView) findViewById(id)).setData(data);
                }
                if (id == R.id.pv_right) {
                    ((PickerView) findViewById(id)).setData(seconds);
                }
                if (id == R.id.pv_work_time) {
                    ((PickerView) findViewById(id)).setData(workTime);
                }
            }
        }

    }

    private OnXDialogItemClickListener onXDialogItemClickListener;

    public interface OnXDialogItemClickListener {
        void OnXDialogItemClick(XDialog xDialog, View view);
    }

    public void setOnXDialogItemClickListener(OnXDialogItemClickListener onXDialogItemClickListener) {
        this.onXDialogItemClickListener = onXDialogItemClickListener;
    }

    @Override
    public void onClick(View v) {
        onXDialogItemClickListener.OnXDialogItemClick(this, v);
        //只要响应了点击，该dialog都会消失
    }


    private OnXDialogItemTouchListener onXDialogItemTouchListener;

    public interface OnXDialogItemTouchListener {
        boolean OnXDialogItemTouch(XDialog xDialog, View view, MotionEvent event);
    }

    public void setOnXDialogItemTouchListener(OnXDialogItemTouchListener onXDialogItemTouchListener) {
        this.onXDialogItemTouchListener = onXDialogItemTouchListener;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return onXDialogItemTouchListener.OnXDialogItemTouch(this, v, event);
    }


    private OnXDialogItemSelectListener onXDialogItemSelectListener;

    public interface OnXDialogItemSelectListener {
        void OnXialogItemSelect(XDialog xDialog, View view, String text);
    }

    public void setOnXDialogItemSelectListener(OnXDialogItemSelectListener onXDialogItemSelectListener) {
        this.onXDialogItemSelectListener = onXDialogItemSelectListener;
    }

    @Override
    public void onSelect(View view, String text) {
        onXDialogItemSelectListener.OnXialogItemSelect(this, view, text);
    }
}
